/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.smwcentral;

import java.util.Map.Entry;

/**
 * Describes an archive and all SPC files within it.
 * 
 * @author Ben
 */
public final class ArchiveDesc {
	private final SpcDesc[] m_tracks;
	private final String m_text;
	
	public final String url;
	public final String title;
	
	ArchiveDesc(String title, CacheEntry archive) {
		this(archive.url, title, archive.entries.iterator());
	}
	
	ArchiveDesc(String url, String title,
							NavigableIterator<Entry<String, byte[]>> archive) {
		this.url = url;
		this.title = title;
		
		final int length = archive.count();
		m_tracks = new SpcDesc[length];
		
		StringBuilder sb = new StringBuilder();
		sb.append(url);
		sb.append(": '");
		sb.append(title);
		sb.append("' (");
		sb.append(length);
		sb.append(" tracks)");
		if (length > 0) sb.append(System.getProperty("line.separator"));
		
		int p = archive.position();
		for (int i = 0; i < length; i++) {
			m_tracks[i] = new SpcDesc(archive.get(i));
			sb.append(System.getProperty("line.separator"));
			sb.append(m_tracks[i].toString());
		}
		archive.setPosition(p); // Save the position. Don't want to mess it up.
		
		m_text = sb.toString();
	}
	
	public boolean isEmpty() {
		return m_tracks.length == 0;
	}
	
	public boolean hasMultipleTracks() {
		return m_tracks.length > 1;
	}
	
	public int numTracks() {
		return m_tracks.length;
	}
	
	public SpcDesc getTrack(int n) {
		return m_tracks[n];
	}

	@Override
	public String toString() {
		return m_text;
	}
}
